// pages/order/demolitionStoveList/demolitionStoveList.js
var app = getApp();
var _PAGE = 1;
var _PAGESIZE = 10;
let load = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    work_order_id: '',
    keyword: '',
    list: [],
    stoveCode: [],//选中的炉具编码

    empty: true,
    hasMore: false,
    loading: false,
    loadShow: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options', options)
    var that = this;
    that.setData({
      work_order_id: options.work_order_id,
      stoveCode: wx.getStorageSync('stoveCode') || [],
    })
    _PAGE = 1;
    that.getList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    _PAGE = 1;
    that.getList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var hasMore = that.data.hasMore;
    if (!hasMore) {
      return false;
    }
    that.setData({
      loading: true
    })
    _PAGE++;
    that.getList(); // 获取列表
  },

  // 获取列表
  getList: function () {
    var that = this;
    app.ajax({
      url: 'Engineer/Workorder/demolitionDataTestingDetails',
      method: "POST",
      load: load,
      msg: '数据加载中...',
      data: {
        work_order_id: that.data.work_order_id,
        keyword: that.data.keyword,
        page: _PAGE,
        pagesize: _PAGESIZE
      },
      success: function (res) {
        if (res.data.code == 1000) {
          var dataList = [];
          if (_PAGE == 1) {
            dataList = res.data.data.energy_saving_data_test;
          } else {
            dataList = that.data.list.concat(res.data.data.energy_saving_data_test);
          }
          if (that.data.stoveCode.length > 0) {
            that.data.stoveCode.map((val) => {
              dataList.map((value) => {
                if (value.array_id == val.array_id) {
                  value.checkbox = true;
                }
              })
            })
          }
          // 是否加载更多
          var hasMore = true;
          if (res.data.data.energy_saving_data_test.length < _PAGESIZE) {
            hasMore = false;
          }
          that.setData({
            list: dataList,
            empty: false,
            hasMore: hasMore,
            loading: false
          });
          load = false;
        } else {
          if (_PAGE == 1) {
            that.setData({
              hasMore: false,
              loading: false,
              empty: true
            })
          } else {
            that.setData({
              hasMore: false,
              loading: false,
              empty: false
            })
          }

        }
        that.setData({
          loadShow: true
        })
      }
    })

  },
  // 输入搜索字段
  inputSearch: function (e) {
    var that = this;
    var val = e.detail.value;
    that.setData({
      keyword: val
    });
  },
  //直接搜索
  searchList: function (e) {
    var that = this;
    var val = e.detail.value;
    _PAGE = 1;
    that.setData({
      keyword: val,
      empty: true,
      hasMore: false,
      loading: false,
      loadShow: false
    })
    load = true;
    that.getList();
  },

  //选择
  chooseStoveCode(e) {
    var that = this;
    let selArr = that.data.stoveCode;
    let selId = e.currentTarget.dataset.id;
    let selName = e.currentTarget.dataset.name;
    let selCode = e.currentTarget.dataset.code;
    let dataList = that.data.list;
    //判断在数组中的下标,是否存在
    let index = -1;
    if (that.data.stoveCode.length > 0) {
      that.data.stoveCode.forEach((item, ind) => {
        if (item.array_id == selId) {
          index = ind;
        }
      })
    }
    if (index < 0) {
      selArr.push({ array_id: selId, stoves_code: selCode, cooker_name: selName });
      dataList.map((value) => {
        if (value.array_id == selId) {
          value.checkbox = true;
        }
      })
    } else {
      dataList.map((value) => {
        if (value.array_id == selId) {
          value.checkbox = false;
        }
      })
      selArr.splice(index, 1);
    }

    that.setData({
      stoveCode: selArr,
      list: dataList
    })
  },
  //确认选择
  sure() {
    var that = this;
    console.log("stoveCode返回", that.data.stoveCode)
    wx.setStorageSync('stoveCode', that.data.stoveCode);
    // 返回上一页
    wx.navigateBack({
      delta: 1
    })
  },
})